<?php

namespace App\Http\Controllers\API;

use App\Criteria\DateTimeCriteria;
use App\Http\Requests\API\CreateSellsAPIRequest;
use App\Http\Requests\API\UpdateSellsAPIRequest;
use App\Models\Sells;
use App\Repositories\SellsRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class SellsController
 * @package App\Http\Controllers\API
 */

class SellsAPIController extends AppBaseController
{
    /** @var  SellsRepository */
    private $sellsRepository;

    public function __construct(SellsRepository $sellsRepo)
    {
        $this->sellsRepository = $sellsRepo;
    }

    /**
     * Display a listing of the Sells.
     * GET|HEAD /sells
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->sellsRepository->pushCriteria(new RequestCriteria($request));
        $this->sellsRepository->pushCriteria(DateTimeCriteria::class);
        $this->sellsRepository->pushCriteria(new LimitOffsetCriteria($request));
        $sells = $this->sellsRepository->paginate($request->limit);
        $res = $sells->toArray();
        foreach ($res['data'] as &$v) {
            $v['money'] = (isset($v['money'])&&!empty($v['money']))?re2float($v['money']/100):'0.00';
        }
        return $this->sendResponse($res, '获取成功');
    }


    /**
     * Display the specified Sells.
     * GET|HEAD /sells/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Sells $sells */
        $sells = $this->sellsRepository->findWithoutFail($id);
        $sells->money = $sells->money/100;
        if (empty($sells)) {
            return $this->sendError('数据不存在');
        }

        return $this->sendResponse($sells->toArray(), '获取成功');
    }


    /**
     * Remove the specified Sells from storage.
     * DELETE /sells/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Sells $sells */
        $sells = $this->sellsRepository->findWithoutFail($id);

        if (empty($sells)) {
            return $this->sendError('数据不存在');
        }

        $sells->delete();

        return $this->sendResponse($id, '操作成功');
    }
}
